package b3.widget.home.unlock
{
	import b3.config.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.basic.*;
	import b3.widget.guide.*;
	
	import com.greensock.*;
	
	import flash.events.*;

	public class UnlockBuildingMovie extends BasicWidget
	{
		private var buildingIndex:int;
		private var currentRoofType:int;
		private var currentUpgrade:int;
		private var oldRoofType:int;
		private var oldUpgrade:int;
		
		public function UnlockBuildingMovie(buildingIndex:int, currentRoofType:int, currentUpgrade:int, oldRoofType:int, oldUpgrade:int)
		{
			super();
			this.buildingIndex = buildingIndex;
			this.currentRoofType = currentRoofType;
			this.currentUpgrade = currentUpgrade;
			this.oldRoofType = oldRoofType;
			this.oldUpgrade = oldUpgrade;
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			var unlockBuildingDialog:UnlockBuildingDialog = new UnlockBuildingDialog(buildingIndex, currentRoofType, currentUpgrade, oldRoofType, oldUpgrade);
			unlockBuildingDialog.x = (BFont.stageWidth - unlockBuildingDialog.width) / 2;
			unlockBuildingDialog.y = (BFont.stageHeight - unlockBuildingDialog.height) / 2;
			addChild(unlockBuildingDialog);
			unlockBuildingDialog.addEventListener(Event.REMOVED_FROM_STAGE, onUnlockBuildingDialogRemoved);
		}
		
		private function onUnlockBuildingDialogRemoved(event:Event):void
		{
			var buildingConfig:BuildingConfig = Config.buildingConfigs[buildingIndex];
			var promptMessage:Array = new Array();
			//首次解锁
			if (oldRoofType == -1)
			{
				promptMessage.push("恭喜你解锁了 " + buildingConfig.name + " ，该建筑最大高度为 " + buildingConfig.height + " 层！");
				promptMessage = promptMessage.concat(buildingConfig.unlockPrompt);
			}
			//解锁新屋顶
			else if (currentRoofType > oldRoofType)
			{
				if (currentRoofType == 1)
				{
					promptMessage.push("恭喜你解锁了 " + buildingConfig.name + " 的高级屋顶，高级屋顶的人口是普通屋顶的 2 倍！");
				}
				else
				{
					promptMessage.push("恭喜你解锁了 " + buildingConfig.name + " 的豪华屋顶，豪华屋顶的人口是普通屋顶的 3 倍！");
				}
			}
			//解锁新高度
			else
			{
				promptMessage.push("恭喜你将 " + buildingConfig.name + " 的高度升级为 +" + (currentUpgrade * 2) + " ，该建筑的最大高度为 " 
					+ (buildingConfig.height + currentUpgrade * 2) + " ！");
			}
			//跳出NPC
			var npcDialog:NpcDialog = new NpcDialog("NpcSecretary1", promptMessage);
			addChild(npcDialog);
			//分享按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var buttonShare:BasicButton = new BasicButton(ButtonBgr, "分享");
			buttonShare.addEventListener(MouseEvent.CLICK, onButtonShareClick);
			npcDialog.talkDialog.addButton(buttonShare);
			npcDialog.addEventListener(Event.REMOVED_FROM_STAGE, onNpcDialogRemoved);
		}
		
		private function onButtonShareClick(event:Event):void
		{
			var buildingConfig:BuildingConfig = Config.buildingConfigs[buildingIndex];
			//首次解锁
			if (oldRoofType == -1)
			{
				ServerRequest.sendFeed("刚刚解锁了 " + buildingConfig.name);
			}
			//解锁新屋顶
			else if (currentRoofType > oldRoofType)
			{
				if (currentRoofType == 1)
				{
					ServerRequest.sendFeed("刚刚解锁了 " + buildingConfig.name + " 的高级屋顶");
				}
				else
				{
					ServerRequest.sendFeed("刚刚解锁了 " + buildingConfig.name + " 的豪华屋顶");
				}
			}
			//解锁新高度
			else
			{
				ServerRequest.sendFeed("刚刚将 " + buildingConfig.name + " 的高度升级为 +" + (currentUpgrade * 2));
			}
			
		}
		
		private function onNpcDialogRemoved(event:Event):void
		{
			TweenLite.delayedCall(0.1, close);
		}
		
	}
}